<template>
    <div class="app-container">
        <!-- 搜索工作栏 -->
        <el-form
            :model="queryParams"
            ref="queryForm"
            size="small"
            :inline="true"
            v-show="showSearch"
            label-width="88px"
        >
            <el-form-item label="落地页名称" prop="landingPageName">
                <el-input
                    v-model="queryParams.landingPageName"
                    placeholder="请输入落地页名称"
                    clearable
                    @keyup.enter.native="handleQuery"
                />
            </el-form-item>
            <el-form-item
                label="操作人"
                prop="userId"
                v-if="roles.includes('super_admin') || roles.includes('admin')"
            >
                <el-select
                    v-model="queryParams.userId"
                    placeholder="请选操作人"
                    clearable
                    filterable
                    @keyup.enter.native="handleQuery"
                >
                    <el-option
                        :label="item.nickname"
                        :value="item.id"
                        v-for="(item, i) in userList"
                        :key="i"
                    ></el-option>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button
                    type="primary"
                    icon="el-icon-search"
                    @click="handleQuery"
                    >搜索</el-button
                >
                <el-button icon="el-icon-refresh" @click="resetQuery"
                    >重置</el-button
                >
            </el-form-item>
        </el-form>
        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button
                    type="primary"
                    plain
                    icon="el-icon-plus"
                    size="mini"
                    @click="openForm({})"
                    >新增</el-button
                >
            </el-col>
            <el-col :span="1.5">
                <el-button
                    plain
                    size="mini"
                    @click="patchDel"
                    type="danger"
                    icon="el-icon-delete"
                    >批量删除</el-button
                >
            </el-col>
        </el-row>
        <el-table
            v-loading="loading"
            :data="list"
            :stripe="true"
            :show-overflow-tooltip="true"
            border
            size="mini"
            max-height="680"
            @selection-change="handleSelectionChange"
        >
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column
                label="编号"
                prop="id"
                width="120"
                align="center"
            />
            <el-table-column
                label="落地页名称"
                prop="landingPageName"
                align="center"
            />
            <el-table-column
                label="落地页模板"
                prop="templateName"
                align="center"
            >
            </el-table-column>

            <el-table-column
                label="落地页地址"
                prop="landingPageUrl"
                align="center"
            >
                <template slot-scope="scope">
                    <el-tag
                        size="mini"
                        @click="copyText(scope.row.landingPageUrl)"
                        class="lk-p"
                        >{{ scope.row.landingPageUrl }}</el-tag
                    >
                </template>
            </el-table-column>
            <el-table-column label="跳转地址" prop="jumpLinkUrl" align="center">
                <template slot-scope="scope">
                    <el-tag
                        size="mini"
                        @click="copyText(scope.row.jumpLinkUrl)"
                        class="lk-p"
                        >{{ scope.row.jumpLinkUrl }}</el-tag
                    >
                </template>
            </el-table-column>
            <el-table-column label="操作人" prop="nickname" align="center">
                <template v-slot="scope">
                    {{
                        scope.row.nickname === "null"
                            ? "--"
                            : scope.row.nickname
                    }}
                </template>
            </el-table-column>
            <el-table-column
                label="操作"
                align="center"
                class-name="small-padding fixed-width"
            >
                <template v-slot="scope">
                    <el-button
                        size="mini"
                        type="text"
                        icon="el-icon-edit"
                        @click="openForm(scope.row)"
                        v-hasPermi="['tk:land:update']"
                        >修改</el-button
                    >

                    <!-- v-hasPermi="['tk:links:delete']" -->
                    <el-button
                        size="mini"
                        type="text"
                        icon="el-icon-delete"
                        @click="handleDelete(scope.row)"
                        v-hasPermi="['tk:land:delete']"
                        >删除</el-button
                    >
                </template>
            </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <pagination
            v-show="total > 0"
            :total="total"
            :page.sync="queryParams.pageNo"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
        />
        <LandForm ref="formRef" @success="getList" />
    </div>
</template>
<script>
import { getLandList, patchDeleteLand } from "@/api/land";
import LandForm from "./landForm";
import { listUser } from "@/api/system/user";
import store from "@/store";
import copy from "copy-to-clipboard";

export default {
    components: {
        LandForm,
    },
    filters: {
        statusFilter(status) {
            const statusMap = {
                1: "模板一",
            };
            return statusMap[status];
        },
    },
    data() {
        return {
            // 显示搜索条件
            showSearch: true,
            queryParams: {
                pageNo: 1,
                pageSize: 10,
                landingPageName: "",
            },
            roles: store.getters.roles,
            ids: [],
            list: [],
            userList: [],
            total: 0,
            loading: false,
        };
    },
    methods: {
        // 复制文本方法
        async copyText(url) {
            try {
                await copy(url);
                this.$message.success("复制成功");
            } catch (err) {
                this.$message.error("复制失败");
            }
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNo = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.$refs["queryForm"].resetFields();
            this.form;
            this.handleQuery();
        },
        /** 添加/修改操作 */
        openForm(row) {
            this.$refs["formRef"].open(row);
        },
        hasSelectdIds() {
            if (this.ids.length === 0) {
                this.$message.info("请选择要操作的项");
            }
            return this.ids.length > 0;
        },
        handleSelectionChange(selection) {
            this.ids = selection.map((item) => item.id);
        },
        patchDel() {
            const hasIds = this.hasSelectdIds();
            if (hasIds) {
                this.$confirm("你是否要删除选中的落地页链接？", "提示", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning",
                })
                    .then(() => {
                        this.pathDelete();
                    })
                    .catch(() => {
                        this.$message({
                            type: "info",
                            message: "已取消删除",
                        });
                    });
            }
        },
        async pathDelete() {
            const { code, msg } = await patchDeleteLand({
                ids: this.ids.join(","),
            });
            if (code === 0) {
                this.$modal.msgSuccess("删除成功");
                this.getList();
            } else {
                this.$modal.msgError(msg);
            }
        },
        /** 删除按钮操作 */
        async handleDelete(row) {
            const id = row.id;
            await this.$modal
                .confirm("你是否要删除该落地页链接？")
                .then(async () => {
                    const res = await patchDeleteLand({
                        ids: id,
                    });
                    if (res.code === 0) {
                        await this.getList();
                        this.$modal.msgSuccess("删除成功");
                    } else {
                        this.$modal.msgError(res.msg);
                    }
                })
                .catch(() => {});
        },
        async getList() {
            this.loading = true;
            const { code, data } = await getLandList(this.queryParams);
            if (code === 0) {
                this.list = data.list;
                this.total = data.total;
            }

            this.loading = false;
        },
        async getUserList(val) {
            const { code, data } = await listUser(val);
            if (code === 0) {
                this.userList = data.list;
            }
        },
    },
    created() {
        this.getList();
        this.getUserList({
            pageNo: 1,
            pageSize: 3000,
        });
    },
};
</script>
<style scoped>
.lk-p {
    cursor: pointer;
    white-space: pre-wrap;
    height: auto;
}
</style>